package br.ufpe.cin.dsoa.qos.monitoring.service;

import java.util.HashMap;
import java.util.Map;

import br.ufpe.cin.dsoa.qos.epcenter.service.EventConsumer;

public class MonitoringConfiguration implements EventConsumer {

	private static long idGenerator = 1;
	private final long id;

	private final MonitoringListener listener;
	private final Map<String, MonitoringConfigurationItem> itens;

	private final String serviceId;
	private final String clientId;

	private synchronized static long getNextId() {
		return idGenerator++;
	}

	public MonitoringConfiguration(String consumerId, String serviceId,
			MonitoringListener listener) {
		this.listener = listener;
		this.clientId = consumerId;
		this.serviceId = serviceId;
		this.itens = new HashMap<String, MonitoringConfigurationItem>();
		this.id = getNextId();
	}

	public MonitoringListener getListener() {
		return listener;
	}

	public String getClientId() {
		return clientId;
	}

	public String getServiceId() {
		return serviceId;
	}

	public void addConfigurationItem(MonitoringConfigurationItem item) {
		this.itens.put(String.valueOf(item.getStatementName()), item);
	}

	public Map<String, MonitoringConfigurationItem> getItens() {
		return itens;
	}

	@Override
	public void receive(Map result, Object userObject, String statementName) {
		//System.out.println("***************   Quebrou   ***************");
		MonitoringConfigurationItem brokenItem = this.itens.get(statementName);
		listener.listen(result, brokenItem, statementName);
	}

	public long getId() {
		return id;
	}

}
